<template>
  <div class="app-container">
      <el-form ref="productForm" :rules="rules" :model="productForm" label-position="left" label-width="120px" style="margin-left:50px;">

        <el-form-item label="标题" prop="title">
          <el-input v-model="productForm.title" />
        </el-form-item>
        <el-form-item label="SEO标题" prop="seoTitle">
          <el-input v-model="productForm.seoTitle" />
        </el-form-item>
        <el-form-item label="SEO关键字" prop="seoKeyword">
          <el-input v-model="productForm.seoKeyword" />
        </el-form-item>
        <el-form-item label="SEO描述" prop="seoDescription">
          <el-input v-model="productForm.seoDescription" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="productForm.price" />
        </el-form-item>
        <el-form-item label="应用" prop="application">
          <el-transfer v-model="productForm.application" :data="applicationOptions"></el-transfer>
        </el-form-item>
        <el-form-item label="介绍" prop="content">
          <el-input v-model="productForm.content" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="介绍" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="productForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveProduct()">
        确认
      </el-button>
      <el-button v-if="productForm.status.value === 5" type="danger" @click="handleDelete(productForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveProduct, getProductDetail, removeProductByID } from '@/api/mechanism/product'
import {  getApplicationByUsable } from '@/api/program/application'


import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      applicationOptions:[],
      productForm: {
        ID: undefined,
        remark: '',
        application:[],
        title: '',
        content:"",
        seoTitle:"",
        seoKeyword:"",
        seoDescription:"",
        price:"",
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadApplicationList();
    let productID = this.$route.query && this.$route.query.productID
    if (productID !== undefined) {
      productID = parseInt(productID)
      if (!isNaN(productID)) {
        this.listLoading = true
        getProductDetail({
          "productID":productID
        }).then(response => {
          if (response.status === 200) {
            this.productForm = response.data
            console.log(this.productForm)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'product' })
    },
    loadApplicationList() {
      let _this = this
      getApplicationByUsable().then(response => {
        if(response.status === 200){
          response.data.list.forEach((row)=>{
            _this.applicationOptions.push({
                key: row['ID'],
                label: row['title']
            })
          });
        }
      })
    },
    saveProduct() {
      let _this = this
      this.$refs['productForm'].validate((valid) => {
        if (valid) {
          try {
            _this.productForm.status = _this.productForm.status.value;
            if(_this.productForm.status == undefined){
              _this.productForm.status = 1;
            }
          } catch (error) {
            _this.productForm.status = 1;
          }
          if(_this.productForm.hasOwnProperty('ID')){
            if(_this.productForm.ID !== undefined){
              _this.productForm.productID = _this.productForm.ID;
            }

          }
          saveProduct(_this.productForm).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'product' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeProductByID(row.ID).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'message' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
